package com.SouthernWall_404.age_tool.common.world.Entity;

import com.SouthernWall_404.age_tool.common.init.ModBlockEntities;
import com.SouthernWall_404.age_tool.common.init.ModBlocks;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.Map;

public class MimicBlockEntity extends ModBlockEntity{

    private static final String MIMICPACK_TAG="mimic";
    private static final String BLOCK_TAG="block";

    private static final String BLOCK_NAME_TAG ="name";
    private static final String PROPERTY_TAG="properties";

    public CompoundTag mimicPack=new CompoundTag();
    private CompoundTag blockPack=new CompoundTag();
    public BlockState mimicBlock= ModBlocks.RIGHTCLICK_TRIGGER_DISPLAY_BLOCK.get().defaultBlockState();
    public boolean hasTracked=false;

    public MimicBlockEntity(BlockEntityType<?> p_155228_, BlockPos p_155229_, BlockState p_155230_) {
        super(p_155228_, p_155229_, p_155230_);
    }

    public MimicBlockEntity(BlockPos blockPos,BlockState blockState)
    {
        this(ModBlockEntities.MIMIC_BLOCK_ENTITY.get(),blockPos,blockState);
    }

    @Override
    protected void saveAdditional(CompoundTag nbt) {
        this.saveBlock();
        this.saveMimicPack();
        super.saveAdditional(nbt);
    }

    @Override
    public void load(CompoundTag nbt) {
        super.load(nbt);
        loadMimicPack();
        loadBlock();
    }

    private void saveMimicPack()
    {
        this.modPack.put(MIMICPACK_TAG,this.mimicPack);
    }

    public void saveBlock()
    {
        if (this.mimicBlock != null) {
            // 将方块状态转换为 CompoundTag
            this.blockPack.putString(BLOCK_NAME_TAG, ForgeRegistries.BLOCKS.getKey(mimicBlock.getBlock()).toString());

            // 如果有方块属性，也保存它们
            if (!mimicBlock.getValues().isEmpty()) {
                CompoundTag propertiesTag = new CompoundTag();
                for (Map.Entry<Property<?>, Comparable<?>> entry : mimicBlock.getValues().entrySet()) {
                    propertiesTag.putString(entry.getKey().getName(), entry.getValue().toString());
                }
                this.blockPack.put(PROPERTY_TAG, propertiesTag);
            }

            this.mimicPack.put(BLOCK_TAG,this.blockPack);
        }
    }

    private void loadMimicPack()
    {

        if(this.modPack.contains(MIMICPACK_TAG))
        {
            this.mimicPack=this.modPack.getCompound(MIMICPACK_TAG);
        }
    }

    public void loadBlock()
    {
        if(this.mimicPack.contains(BLOCK_TAG))
        {
            this.blockPack=this.mimicPack.getCompound(BLOCK_TAG);
            if(this.blockPack!=null)
            {
                ResourceLocation blockId = new ResourceLocation(this.blockPack.getString(BLOCK_NAME_TAG));
                Block block = ForgeRegistries.BLOCKS.getValue(blockId);
                BlockState state = block.defaultBlockState();

                if (this.blockPack.contains(PROPERTY_TAG)) {
                    CompoundTag propertiesTag = this.blockPack.getCompound(PROPERTY_TAG);
                    for (String key : propertiesTag.getAllKeys()) {
                        Property<?> property = block.getStateDefinition().getProperty(key);
                        if (property != null) {
                            state = setValueHelper(state, property, propertiesTag.getString(key));
                        }
                    }
                }
                this.mimicBlock=state;
            }
        }
    }

    private <T extends Comparable<T>> BlockState setValueHelper(BlockState state, Property<T> property, String value) {
        return property.getValue(value).map(v -> state.setValue(property, v)).orElse(state);
    }

    // 设置存储的方块状态
    public void setStoredBlockState(BlockState state) {
        this.mimicBlock = state;
        setChanged(); // 标记数据已更改，需要保存
    }

    // 获取存储的方块状态
    public BlockState getStoredBlockState() {
        return this.mimicBlock;
    }
}
